package cn.itsource.pet.service.impl;

import cn.itsource.basic.dto.OrderDto;
import cn.itsource.basic.service.impl.BaseImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageResult;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.TEmployee;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.OrderPetAcquisitionQuery;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private LoginContext loginContext;

    @Override
    public PageResult<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        TEmployee employee = (TEmployee)loginContext.getLoginObject(request);
        if(employee.getShopId() != null){//店铺管理人员[店员和店长]
            query.setShopId(employee.getShopId());
            //如果当前员工的id != shop的adminid => 店员
            Shop shop = shopMapper.selectById(employee.getShopId());
            if(employee.getId() != shop.getAdminId()){
                query.setEmpId(employee.getId());
            }
        }
        return super.selectByQueryPage(query);
    }



    @Override
    public AjaxResult comfirm(OrderDto orderDto) {
        //首先获取参数中所有属性的值
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer paytype = orderDto.getPaytype();
        //生成宠物信息并加入数据库
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectById(orderId);
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(orderPetAcquisition.getSearchMasterMsgId());
        Pet pet = searchMasterMsgToPet(searchMasterMsg ,money);
        petMapper.insert(pet);//添加后获得自增id

        //修改寻主信息状态为2
        searchMasterMsg.setState(2);
        searchMasterMsgMapper.update(searchMasterMsg);

        //修改订单状态为1并设置金额，支付方式，宠物id
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setPaytype(paytype);
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        return new AjaxResult();
    }

    private Pet searchMasterMsgToPet(SearchMasterMsg searchMasterMsg, BigDecimal money) {
        Pet pet = new Pet();
        //设置参数
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(money);
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setResources(searchMasterMsg.getResources());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setUserId(searchMasterMsg.getUserId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petMapper.insert(pet);
        return pet;
    }


    @Override
    public AjaxResult cancle(Long id) {
        //取消   修改寻主消息状态   --订单放入寻主池
        orderPetAcquisitionMapper.updateState(-1,id);
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectById(id);
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearchMasterMsgId());
        return new AjaxResult();
    }
}
